package de.dopichaj.labrador.search.hit;

import java.io.File;

import org.apache.log4j.Logger;

/**
 * A Hit that stores all information in main memory.
 */
public final class MemoryHit extends AbstractHit {
    
    private static final Logger log = Logger.getLogger(MemoryHit.class);
    
    private final File file;
    
    private final String[] xPathParts;
    
    private final int position;

    private final int contentLength;
    
    public MemoryHit(File file, String[] xPathParts, int length, int position, float score) {
        
        this.file = file;
        this.xPathParts = new String[xPathParts.length];
        for (int i = 0; i < xPathParts.length; i++) {
            this.xPathParts[i] = xPathParts[i].intern();
        }
        this.contentLength = length;
        this.position = position;
        setScore(score);
    }

    /**
     * Constructs a hit.
     */
    public MemoryHit(File file, String xPath, int length, int position, float score) {
        this(file, xPath.substring(1).split("/"), length, position, score);
    }

    /**
     * Constructs a hit.
     */
    public MemoryHit(File file, String xPath, int length, int position) {
        this(file, xPath, length, position, 0.0f);
    }
    
    /**
     * Construct a dummy-hit for merging sibling results to a nonexistent parent.
     */
    public MemoryHit(File file, String xPath) {
        this(file, xPath, 0, 0);
        
        // Basically missing parent nodes should not occur, but can be processed.
        // Therefore we write a warning message.
        if (log.isDebugEnabled()) {
            log.debug("Constructing a dummy node for " + xPath + "("+file+")");
        }
    }

    /* (non-Javadoc)
     * @see de.dopichaj.labrador.search.hit.Hit#getXPath()
     */
    public String getXPath() {
        final StringBuilder builder = new StringBuilder();
        for (final String part : xPathParts) {
            builder.append("/").append(part);
        }
        return builder.toString(); 
    }
    
    public String[] getXPathParts() {
        return xPathParts;
    }

    @Override
    public String getEnclosingXPath() {
        final StringBuilder builder = new StringBuilder();
        for (int i = 0; i < xPathParts.length - 1; i++) {
            builder.append("/").append(xPathParts[i]);
        }
        return builder.toString(); 
    }

    /* (non-Javadoc)
     * @see de.dopichaj.labrador.search.hit.Hit#getFile()
     */
    public File getFile() {
        return file;
    }
    
    /* (non-Javadoc)
     * @see de.dopichaj.labrador.search.hit.Hit#getContentLength()
     */
    public int getContentLength() {
        return contentLength;
    }
    
    /* (non-Javadoc)
     * @see de.dopichaj.labrador.search.hit.Hit#getPosition()
     */
    public int getPosition() {
        return position;
    }

    public int getDepth() {
        return xPathParts.length - 1;
    }
}
/*
Copyright (c) 2005 Benedikt Eger
Copyright (c) 2005-2007 Philipp Dopichaj

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/